package com.example.xd.yunshixun;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.chinamobile.ysx.YSXImConfig;
import com.chinamobile.ysx.YSXSdk;
import com.chinamobile.ysx.YSXSdkInitializeListener;
import com.chinamobile.ysx.auther.LoginResult;
import com.chinamobile.ysx.auther.YSXLoginResultListener;
import com.chinamobile.ysx.iminterface.IMOflineLinePushConfig;
import com.chinamobile.ysx.iminterface.ImConnectionListener;
import com.chinamobile.ysx.iminterface.ImMessageListener;
import com.chinamobile.ysx.iminterface.InviteMeeting;

import static com.example.xd.yunshixun.Constance.APP_KEY;
import static com.example.xd.yunshixun.Constance.APP_SECRET;
import static com.example.xd.yunshixun.Constance.Sp_token;
import static com.example.xd.yunshixun.Constance.XD_account;
import static com.example.xd.yunshixun.Constance.XD_secret;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener, YSXLoginResultListener {

    private EditText mEditText;
    private EditText mEdtPassword;
    private String mSdktoken;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        findViewById(R.id.btn_login_sdk).setOnClickListener(this);
        mEditText = findViewById(R.id.edtUserName);
        mEdtPassword = findViewById(R.id.edtPassword);

        mEditText.setText(XD_account);
        mEdtPassword.setText(XD_secret);
    }


    @Override
    public void onClick(View view) {
        login(mEditText.getText().toString(), mEdtPassword.getText().toString());
    }

    private void login(String name, String pwd) {
        YSXSdk ysxSdk = YSXSdk.getInstance();
        if (ysxSdk.isInitialized()) {
            loginsdk(ysxSdk, name, pwd);
        } else {
            Toast.makeText(this, "sdk初始化失败", Toast.LENGTH_SHORT).show();
        }

    }

    private void loginsdk(YSXSdk ysxSdk, String name, String pwd) {
        ysxSdk.loginTask(name, pwd, APP_KEY, APP_SECRET, this);
    }

    @Override
    public void onLoginResult(LoginResult loginResult) {
        mSdktoken = loginResult.sdktoken;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(mSdktoken)) {
                    Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                } else {
                    SPUtils.putString(LoginActivity.this, Sp_token, mSdktoken);
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                }
            }
        });

    }
}
